package com.fq.myapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fq.myapplication.R;
import com.fq.myapplication.bean.CommentItem;

import java.util.List;

public class CommentAdapter extends BaseAdapter {
    private Context context;
    private List<CommentItem> dataList;
    private LayoutInflater inflater;

    public CommentAdapter(Context context, List<CommentItem> dataList) {
        this.context = context;
        this.dataList = dataList;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return dataList != null ? dataList.size() : 0;
    }

    @Override
    public CommentItem getItem(int position) {
        return dataList != null ? dataList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static class ViewHolder {
        ImageView avatarView;
        TextView usernameView;
        TextView timeView;
        TextView contentView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.fragment_comment_item, parent, false);
            holder = new ViewHolder();
            holder.avatarView = convertView.findViewById(R.id.item_avatar);
            holder.usernameView = convertView.findViewById(R.id.item_username);
            holder.timeView = convertView.findViewById(R.id.item_time);
            holder.contentView = convertView.findViewById(R.id.item_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        CommentItem item = getItem(position);
        if (item != null) {
            holder.usernameView.setText(item.getUsername());
            holder.timeView.setText(item.getTime());
            holder.contentView.setText(item.getContent());
            // 如果有需要，这里可以使用图片加载库（如Glide）来加载头像
            holder.avatarView.setImageResource(R.drawable.avatar);
        }

        return convertView;
    }
}
